package com.dcy.znoa.controller.publiz;

import com.dcy.znoa.common.annotation.AuthenticationAPI;
import com.dcy.znoa.common.controller.BaseController;
import com.dcy.znoa.common.exception.BusinessException;
import com.dcy.znoa.common.util.pojo.JsonCommon;
import com.dcy.znoa.common.util.pojo.JsonForList;
import com.dcy.znoa.controller.publiz.model.canteen.AddCanteenModel;
import com.dcy.znoa.controller.publiz.model.canteen.LogicDelCanteenModel;
import com.dcy.znoa.controller.publiz.model.canteen.SelectCanteenModel;
import com.dcy.znoa.controller.publiz.model.canteen.UpdateCanteenModel;
import com.dcy.znoa.domain.publiz.Canteen;
import com.dcy.znoa.service.publiz.CanteenService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

import static com.dcy.znoa.common.util.pojo.JsonCommon.*;

/**
 * 食堂 controller
 * @author pengyu
 */
@Controller
@RequestMapping("canteen")
public class CanteenController extends BaseController {
    @Autowired
    CanteenService canteenService;

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/selectList")
    public JsonForList selectList(@Valid SelectCanteenModel model,BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (bindingResult.hasErrors()){
            return  new JsonForList("404",bindingResult.getFieldError().getDefaultMessage());
        }
        return canteenService.selectList(model);
//        List list = canteenService.selectList(model);
//        if(list.isEmpty()){
//            return new JsonForList(NOT_FOUND_CODE);
//        }else{
//            return new JsonForList(list);
//        }
    }


    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/insert")
    public JsonCommon insert(@Valid AddCanteenModel model, BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (bindingResult.hasErrors()){
            return  new JsonCommon("404",bindingResult.getFieldError().getDefaultMessage());
        }
        if(model.getEntityType()==null){
            model.setEntityType(1);//	所属类型 1 园区 2 企业
        }
        model.setCreateOperId(getCurrentUser().getOperId());
        Canteen cp = new Canteen();
        BeanUtils.copyProperties(cp, model);
        Integer result =  canteenService.insert(cp);
        if(result>0){
            return new JsonCommon(SUCCESS_CODE);
        }else{
            return new JsonCommon(INSERT_ERROR_CODE);
        }
    }

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/updateById")
    public JsonCommon updateById(@Valid UpdateCanteenModel model, BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (bindingResult.hasErrors()){
            return  new JsonCommon("404",bindingResult.getFieldError().getDefaultMessage());
        }
        model.setUpdateOperId(getCurrentUser().getOperId());
        Canteen cp = new Canteen();
        BeanUtils.copyProperties(cp, model);
        Integer result =  canteenService.updateById(cp);
        if(result>0){
            return new JsonCommon(SUCCESS_CODE);
        }else{
            return new JsonCommon(UPDATE_ERROR_CODE);
        }
    }

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/valid")
    public JsonCommon valid(@Valid LogicDelCanteenModel model, BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (bindingResult.hasErrors()){
            return  new JsonCommon("404",bindingResult.getFieldError().getDefaultMessage());
        }
        model.setUpdateOperId(getCurrentUser().getOperId());
        Canteen cp = new Canteen();
        BeanUtils.copyProperties(cp, model);
        Integer result =  canteenService.updateById(cp);
        if(result>0){
            return new JsonCommon(SUCCESS_CODE);
        }else{
            return new JsonCommon(DELETE_ERROR_CODE);
        }
    }
}
